package com.chenyitun.cloudadmin.common.controller;

import com.bootdo.common.domain.DictData;
import com.bootdo.common.domain.DictType;
import com.bootdo.common.service.DictDataService;
import com.bootdo.common.service.DictTypeService;
import com.bootdo.common.utils.bootdo.PageUtils;
import com.bootdo.common.utils.bootdo.Query;
import com.bootdo.common.utils.bootdo.R;
import com.bootdo.common.utils.poi.ExcelUtil;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典数据表
 * 
 * @author admin
 * @email chen@163.com
 * @date 2019-02-15 09:16:13
 */
 
@Controller
@RequestMapping("/common/dictData")
public class DictDataController  extends BaseController  {
	@Autowired
	private DictDataService dictDataService;
	@Autowired
	private DictTypeService dictTypeService;

	@GetMapping()
	@RequiresPermissions("common:dictData:dictData")
	String DictData(@RequestParam Map<String, Object> params , Model model){
		DictType dictType =  dictTypeService.getByType(params.get("type").toString()) ;
		model.addAttribute( "dictType" , dictType);
	    return "common/dictData/dictData";
	}
	
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("common:dictData:dictData")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<DictData> dictDataList = dictDataService.list(query);
        int total = dictDataService.count(query);
        PageUtils pageUtils = new PageUtils(dictDataList, total);
        return pageUtils;
    }


	@GetMapping("/add")
	@RequiresPermissions("common:dictData:add")
	String add(@RequestParam Map<String, Object> params , Model  model){
	    model.addAttribute( "params" , params );
	    return "common/dictData/add";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:dictData:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DictData dictData = dictDataService.get(id);
		model.addAttribute("dictData", dictData);
	    return "common/dictData/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:dictData:add")
	public R save( DictData dictData){
        if( !beanValidator(dictData )){
            return R.error();
        }
		if(dictDataService.save(dictData)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:dictData:edit")
	public R update( DictData dictData){
		dictDataService.update(dictData);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("common:dictData:remove")
	public R remove( Long id){
		if(dictDataService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:dictData:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		dictDataService.batchRemove(ids);
		return R.ok();
	}

    /**
     * 导出列表
     */
    @RequiresPermissions("common:dictData:export")
    @PostMapping("/export")
    @ResponseBody
    public R export(@RequestParam Map<String, Object> params) {
        List<DictData> list = dictDataService.list(params);
        ExcelUtil<DictData> util = new ExcelUtil<DictData>(DictData.class);
        return util.exportExcel(list, "dictData");

    }

//    /**
//     * 转换
//     */
//    @RequiresPermissions("common:dictData:no")
////    @GetMapping("/transfer")
//    @ResponseBody
//    public R transfer() {
//        List<Dict> dictlist = dictService.list(Maps.newHashMap());
//		Set<String> typeSet = new HashSet<>();
//        for( int i = 0 ; i < dictlist.size() ; i ++ ){
//			typeSet.add(dictlist.get(i).getType());
//		}
//		for( String type: typeSet){
//        	Dict dict = dictService.listByType(type).get(0);
//        	DictType dictType = new DictType();
//        	dictType.setName(dict.getDescription());
//        	dictType.setType(type);
//			dictTypeService.save(dictType);
//		}
//		for( int i = 0 ; i < dictlist.size() ; i ++ ){
//			DictData  dictData = new DictData();
//			if(dictlist.get(i).getSort() != null)
//				dictData.setDictSort(dictlist.get(i).getSort().intValue());
//			dictData.setName(dictlist.get(i).getName());
//			dictData.setType(dictlist.get(i).getType());
//			dictData.setValue(dictlist.get(i).getValue());
//			dictDataService.save(dictData);
//		}
//		return R.ok();
//    }

	@RequiresPermissions("sail:bill:bill")
	@GetMapping("/getAreas")
	@ResponseBody
	public R getAreas(){
    	Map<String , Object> params = Maps.newHashMap();
    	params.put("type" , "sail_bill_deploy_area");
    	List<DictData>  dataList = dictDataService.list(params);
		return R.ok()
				.put("data" , dataList);
	}
}
